package wangjg.test.aop;


import java.lang.reflect.Method;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppContext {

	private static ApplicationContext context;
	static{
	    String[] files = { "applicationContext*.xml" };   
	    context = new ClassPathXmlApplicationContext(files);
	}
	
	public static Object getBean(Class cls){
		Map map=context.getBeansOfType(cls);
		if(map==null||map.size()==0){
			return null;
		}
		if(map.size()>1){
			new Exception("bean is not unique.").printStackTrace();
		}
		return map.values().iterator().next();
	}
	
	public static Object getBean(String name){
		return context.getBean(name);
	}
	
	public static void execute(Object obj,String methodName,String args[]){
		try {
			Method method=obj.getClass().getMethod(methodName, String[].class);
			method.invoke(obj, args);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void exit(){
		System.exit(0);
	}

	public static void main(String[] args) {
		TestService ts=(TestService) AppContext.getBean(TestService.class);
		ts.test();
	}
}
